import {identity4d} from "@/views/cg/lib/matrix/indentity.ts";
import {multiply4} from "@/views/cg/lib/matrix/multipy.ts";
import {rotateX, rotateY, rotateZ} from "@/views/cg/lib/matrix/rotate.ts";
import {translate3d} from "@/views/cg/lib/matrix/translate.ts";
import {scale3d} from "@/views/cg/lib/matrix/scale.ts";
import {perspective} from "@/views/cg/lib/matrix/project.ts";

// Facade 设计模式  链式调用
export class Mat4{
  constructor() {
    this.mat = identity4d()
  }

  rotate(ax, ay, az){
    if(ax!==0){
      this.mat = multiply4(this.mat, rotateX(ax))
    }

    if(ay !== 0){
      this.mat = multiply4(this.mat, rotateY(ay))
    }

    if(az !== 0){
      this.mat = multiply4(this.mat, rotateZ(az))
    }

    return this
  }

  translate(x,y,z){
    this.mat = multiply4(this.mat, translate3d(x,y,z))
    return this
  }

  perspective(fov, aspect, zNear, zFar){
    this.mat = multiply4(this.mat, perspective(fov, aspect,zNear,zFar))
    return this
  }

  scale(sx,sy,sz){
    this.mat = multiply4(this.mat, scale3d(sx, sy, sz))
    return this
  }

  lookAt(x,y,z, ax,ay,az){
    // console.log('this.mat', this.mat)
    // console.log('this.translate(-x,-y,-z)', this.translate(-x,-y,-z))

    this.mat = multiply4(this.mat, translate3d(-x,-y,-z))
    this.rotate(-ax,-ay,-az)
    return this
  }

  getMatrix(){
    return this.mat
  }
}
